package com.lingdiman.ocr;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.lingdiman.ocr.adapter.ContentAdapter;
import com.lingdiman.ocr.adapter.IndicatorAdapter;
import com.lingdiman.ocr.layoutmanager.GalleryLayoutManager;
import com.lingdiman.ocr.transformer.Transformer;
import com.nhh.evidenceSdk.EvidenceSdk;
import com.nhh.evidenceSdk.callback.EvidenceGjjRecognizedCallback;
import com.nhh.evidenceSdk.callback.EvidenceHbRecognizedCallback;
import com.nhh.evidenceSdk.callback.EvidenceWldRecognizedCallback;
import com.nhh.evidenceSdk.callback.EvidenceZfbRecognizedCallback;
import com.nhh.evidenceSdk.http.model.EvidenceZfbResult;
import com.nhh.evidenceSdk.http.model.GjjResult;
import com.nhh.evidenceSdk.http.model.WechatHbResult;
import com.nhh.evidenceSdk.http.model.WldResult;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class FlowDescriptionActivity extends AppCompatActivity {

    public static final String TYPE_ZFB = "zfb";
    public static final String TYPE_GJJ = "gjj";
    public static final String TYPE_WLD = "wld";
    public static final String TYPE_WECHAT_HB = "wechatHb";

    private ImageView mBackIv;
    private TextView mTitleTv, mStepNumTv, mStepDesTv, mBottomTv;
    private RecyclerView mIndicatorRv, mRv;

    private List<Integer> mDatas = new ArrayList<>();

    private ContentAdapter mAdapter;

    private String[] stepsDes;

    private String type;

    public static Intent getLauncherIntent(Context context, String type) {
        Intent intent = new Intent(context, FlowDescriptionActivity.class);
        intent.putExtra("type", type);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow_description);
        //init方法调用后会申请录屏权限，不需要提前调用init方法建议在需要使用sdk功能的页面调用进行初始化，提前申请录屏权限
        //openLog、setBaseurl、setValidTimeout 等方法需要在init方法前调用，确保对应配置生效

        type = getIntent().getStringExtra("type");

        mBackIv = findViewById(R.id.back_iv);
        mTitleTv = findViewById(R.id.title_tv);
        mStepNumTv = findViewById(R.id.step_num_tv);
        mStepDesTv = findViewById(R.id.step_des_tv);
        mBottomTv = findViewById(R.id.btn);

        mIndicatorRv = findViewById(R.id.indicator_rv);
        mRv = findViewById(R.id.rv);


        initData(type);

        mAdapter = new ContentAdapter();
        mRv.setAdapter(mAdapter);
        mAdapter.setData(mDatas);

        GalleryLayoutManager manager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
        manager.attach(mRv);
        manager.setItemTransformer(new Transformer());

        IndicatorAdapter adapter = new IndicatorAdapter();
        mIndicatorRv.setAdapter(adapter);
        adapter.setData(mDatas);

        manager.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() {
            @Override
            public void onItemSelected(RecyclerView recyclerView, View item, int position) {
                adapter.setSelectPos(position);
                mStepDesTv.setText(stepsDes[position]);
                mStepNumTv.setText(getString(R.string.step_template, numberConvertChinese(position + 1)));
            }
        });

        mBottomTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
            }
        });
        mBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TYPE_ZFB.equals(type)) {
                    backPrePage(mZfbResult);
                }
                if (TYPE_GJJ.equals(type)) {
                    backPrePage(mGjjResult);
                }
                if (TYPE_WLD.equals(type)) {
                    backPrePage(mWldResult);
                }
            }
        });
    }

    private static final String TAG = "FlowDescriptionActivity";

    private int mRequestCode;
    private int mResultCode;
    private Intent mData;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, String.format("onActivityResult: ---->requestCode:  %s,  resultCode:  %s,  data:  %s", requestCode, resultCode, data));
        mRequestCode = requestCode;
        mResultCode = resultCode;
        mData = data;
        if (mRequestCode == EvidenceSdk.REQUEST_CODE && mResultCode == RESULT_OK && mData != null) {
            //表示已授权可以跳转
            EvidenceSdk.getInstance().onActivityResult(requestCode, resultCode, data);
            startRecognized();
        } else {
            EvidenceSdk.getInstance().release(this);
            Toast.makeText(getApplicationContext(), "请点击授权开始录制，同意授权后重试", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ------------------->release");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (intent.getBooleanExtra("success", false)) {
            //            认证成功返回上级页面
            Log.d(TAG, "onNewIntent: 认证成功返回-----》");
            Serializable result = mZfbResult;
            if (TYPE_GJJ.equals(type)) {
                result = mGjjResult;
            }
            if (TYPE_WLD.equals(type)) {
                result = mWldResult;
            }
            backPrePage(result);
        }
        super.onNewIntent(intent);
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy: ----------------->release");
        EvidenceSdk.getInstance().release(this);
        super.onDestroy();
    }

    private void click() {
        EvidenceSdk.getInstance().openLog(BuildConfig.DEBUG).setBackAction("evd.result.backPage").init(this, "123");
    }

    private EvidenceZfbResult mZfbResult;
    private GjjResult mGjjResult;
    private WldResult mWldResult;

    private void startRecognized() {
        if (TYPE_ZFB.equals(type)) {
            EvidenceSdk.getInstance().recognizeZfbInfo(this, new EvidenceZfbRecognizedCallback() {
                @Override
                public void onSuccess(EvidenceZfbResult result) {
                    mZfbResult = result;
                }
            });
            return;
        }
        if (TYPE_GJJ.equals(type)) {
            EvidenceSdk.getInstance().recognizeGjjInfo(this, new EvidenceGjjRecognizedCallback() {
                @Override
                public void onSuccess(GjjResult result) {
                    mGjjResult = result;
                }
            });
            return;
        }
        if (TYPE_WLD.equals(type)) {
            EvidenceSdk.getInstance().recognizeWldInfo(this, new EvidenceWldRecognizedCallback() {
                @Override
                public void onSuccess(WldResult result) {
                    mWldResult = result;
                }
            });
            return;
        }
        if (TYPE_WECHAT_HB.equals(type)) {
            EvidenceSdk.getInstance().recognizeHbInfo(this, new EvidenceHbRecognizedCallback() {
                @Override
                public void onSuccess(WechatHbResult result) {

                }
            });
        }
    }

    private void backPrePage(Serializable result) {
        Intent data = new Intent();
        data.putExtra("type", type);
        data.putExtra("data", result);
        setResult(RESULT_OK, data);
        finish();
    }

    private void openWithZfb(EvidenceZfbResult result) {
        Intent data = new Intent(this, MainActivity.class);
        data.putExtra("type", type);
        data.putExtra("data", result);
        startActivity(data);
    }

    private void openWithGjj(GjjResult result) {
        Intent data = new Intent(this, MainActivity.class);
        data.putExtra("type", type);
        data.putExtra("data", result);
        startActivity(data);
    }

    private void openWithWld(WldResult result) {
        Intent data = new Intent(this, MainActivity.class);
        data.putExtra("type", type);
        data.putExtra("data", result);
        startActivity(data);
    }

    private void initData(String type) {
        if (TYPE_ZFB.equals(type)) {
            mDatas.add(R.mipmap.zfb_1);
            mDatas.add(R.mipmap.zfb_2);
            mDatas.add(R.mipmap.zfb_3);
            mDatas.add(R.mipmap.zfb_4);
            mDatas.add(R.mipmap.zfb_5);
            stepsDes = getResources().getStringArray(R.array.zfb_des);
            mTitleTv.setText("支付宝");
            return;
        }
        if (TYPE_GJJ.equals(type)) {
            mDatas.add(R.mipmap.gjj_1);
            mDatas.add(R.mipmap.gjj_2);
            mDatas.add(R.mipmap.gjj_3);
            mDatas.add(R.mipmap.gjj_4);
            mDatas.add(R.mipmap.gjj_5);
            mDatas.add(R.mipmap.gjj_6);
            mDatas.add(R.mipmap.gjj_7);
            stepsDes = getResources().getStringArray(R.array.gjj_des);
            mTitleTv.setText("公积金");
            return;
        }
        if (TYPE_WLD.equals(type)) {
            mDatas.add(R.mipmap.wld_1);
            mDatas.add(R.mipmap.wld_2);
            mDatas.add(R.mipmap.wld_3);
            mDatas.add(R.mipmap.wld_4);
            mDatas.add(R.mipmap.wld_5);
            mDatas.add(R.mipmap.wld_6);
            stepsDes = getResources().getStringArray(R.array.wld_des);
            mTitleTv.setText("微粒贷");
            return;
        }
        if (TYPE_WECHAT_HB.equals(type)) {
            mDatas.add(R.mipmap.wld_1);
            mDatas.add(R.mipmap.hb_2);
            mDatas.add(R.mipmap.hb_3);
            mDatas.add(R.mipmap.hb_4);
            mDatas.add(R.mipmap.hb_5);
            mDatas.add(R.mipmap.hb_6);
            mDatas.add(R.mipmap.hb_7);
            stepsDes = getResources().getStringArray(R.array.hb_des);
            mTitleTv.setText("还呗");
        }
    }

    private String numberConvertChinese(int number) {
        //数字对应的汉字
        String[] num = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        //单位
        String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
        //将输入数字转换为字符串
        String result = String.valueOf(number);
        //将该字符串分割为数组存放
        char[] ch = result.toCharArray();
        //结果 字符串
        String str = "";
        int length = ch.length;
        for (int i = 0; i < length; i++) {
            int c = (int) ch[i] - 48;
            if (c != 0) {
                str += num[c - 1] + unit[length - i - 1];
            }
        }
        return str;
    }
}
